<template>
  <div class="sellList">
    <el-card class="taskList">
      <el-form class="s_form" :inline="true">
        <div class="left">
          <el-form-item>
            <div class="cardName">车辆成交订单</div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="small" @click="sellAdd">新增订单</el-button>
          </el-form-item>
        </div>
        <div class="right">
          <el-form-item>
            <el-date-picker size="small" v-model="dataForm.time" type="datetimerange" :picker-options="pickerOptions"
              range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" align="right"
              value-format="timestamp" @change="getDataList">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-input size="small" v-model="dataForm.customerName" placeholder="请输入关键字" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="small" @click="getDataList">查询</el-button>
          </el-form-item>
        </div>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border :height="tabHeight">
        <el-table-column v-for="row in headerList" :key="row.key" :prop="row.key" :min-width="row.width"
          :label="row.label" show-overflow-tooltip header-align="center" align="center"></el-table-column>
        <el-table-column min-width="150px" label="购买保险" show-overflow-tooltip header-align="center" align="center">
          <template slot-scope="scope">
            <div v-for="item in scope.row.carInsuranceVOList" :key="item.id">{{item.carInsuranceType}}</div>
          </template>
        </el-table-column>
        <!-- <el-table-column label="操作" fixed="right" header-align="center" align="center" width="200">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="sellDetail(scope.row)">查看详情</el-button>
          </template>
        </el-table-column> -->
      </el-table>
      <el-pagination :current-page.sync="curPage" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total"
        layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
    </el-card>
    <el-dialog width="80%" :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
    <sell-dialog ref="sellDialog" @refresh="getDataList"/>
  </div>
</template>

<script>
import sellDialog from './sellComponent/sellDialog.vue'
export default {
  components: { sellDialog },
  data () {
    return {
      dataListLoading: false,
      dialogImageUrl: '',
      dialogVisible: false,
      dataForm: {
        type: '2',
        time: [],
        customerName: ''
      },
      dataList: [],
      // 资料列表字段
      headerList: [{
        key: 'id',
        label: '订单编号',
        width: '150'
      },
      {
        key: 'carNumber',
        label: '车辆编号',
        width: '150'
      },
      {
        key: 'carModel',
        label: '车型',
        width: '150'
      },
      {
        key: 'customerName',
        label: '客户名称',
        width: '150'
      },
      {
        key: 'customerPhone',
        label: '客户电话',
        width: '150'
      },
      {
        key: 'finalPrices',
        label: '最终成交价(元)',
        width: '150'
      },
      {
        key: 'remarks',
        label: '购车备注',
        width: '150'
      },
      {
        key: 'createDate',
        label: '购买时间',
        width: '150'
      }],
      tabHeight: '600',
      pickerOptions: {
        shortcuts: [{
          text: '本月',
          onClick (picker) {
            let oneDayLong = 24 * 60 * 60 * 1000
            let now = new Date()
            let year = now.getFullYear()
            let monthStartDate = new Date(year, now.getMonth(), 1) // 当前月1号
            let nextMonthStartDate = new Date(year, now.getMonth() + 1, 1) // 下个月1号
            let days = (nextMonthStartDate.getTime() - monthStartDate.getTime()) / oneDayLong // 计算当前月份的天数
            let monthEndDate = new Date(year, now.getMonth(), days, 23, 59, 59)
            console.log([monthStartDate, monthEndDate])
            picker.$emit('pick', [monthStartDate, monthEndDate])
          }
        }, {
          text: '上个月',
          onClick (picker) {
            let oneDayLong = 24 * 60 * 60 * 1000
            let now = new Date()
            let year = now.getFullYear()
            let monthStartDate = new Date(year, now.getMonth() - 1, 1) // 当前月1号
            let nextMonthStartDate = new Date(year, now.getMonth(), 1) // 下个月1号
            let days = (nextMonthStartDate.getTime() - monthStartDate.getTime()) / oneDayLong // 计算当前月份的天数
            let monthEndDate = new Date(year, now.getMonth() - 1, days, 23, 59, 59)
            picker.$emit('pick', [monthStartDate, monthEndDate])
          }
        }, {
          text: '最近一个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      // 分页
      curPage: 1,
      total: 0,
      limit: 10
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.tabHeight = this.$getClientHeight() - 300 + 'px'
    })
  },
  activated () {
    this.getDataList()
  },
  methods: {
    // 新增销售
    sellAdd () {
      this.$refs.sellDialog.init()
    },
    // 查看详情
    sellDetail (row) {
      console.log('查看详情')
      this.$refs.sellDialog.init(row)
    },
    // 获取数据
    getDataList () {
      this.dataListLoading = true
      this.$http.get('/cardeal/list', {
        params: {
          limit: this.limit,
          page: this.curPage,
          customerName: this.dataForm.customerName,
          startTime: (this.dataForm.time ? this.dataForm.time[0] : null),
          endTime: this.dataForm.time ? this.dataForm.time[1] : null
        }
      }).then(({ data: res }) => {
        let { code, msg, page } = res
        console.log(code, msg, page)
        this.dataList = []
        this.total = 0
        if (code === 0 && page) {
          let { list, total } = page
          this.dataList = list && list.length ? list : []
          this.total = total
          this.dataListLoading = false
        }
        if (code !== 0) {
          this.$message.error(msg)
        }
        this.dataListLoading = false
      })
    },
    // 切换每页数量
    pageSizeChangeHandle (size) {
      console.log(size)
      this.limit = size
      this.curPage = 1
      this.getDataList()
    },
    // 切换页
    pageCurrentChangeHandle (curPage) {
      this.curPage = curPage
      this.getDataList()
    }
  }
}
</script>

<style lang="scss">
.sellList {
  padding: 20px;
  .taskList {
    width: 100%;
    min-width: 1100px;
    background-color: rgba(255, 255, 255, 1);
    margin-top: 15px;
    .s_form {
      display: flex;
      justify-content: space-between;
      .cardName {
        font-weight: bold;
        font-size: 18px;
      }
      .s_input {
        width: 100px;
      }
    }
  }
}
</style>
